package com.wangc.report.service.impl;

import com.wangc.report.dao.ConfigMapper;
import com.wangc.report.dao.LoadMapper;
import com.wangc.report.entity.ConfigEntity;
import com.wangc.report.entity.LoadEntity;
import com.wangc.report.entity.TaskLogEntity;
import com.wangc.report.service.LoadService;
import com.wangc.report.service.TaskLogService;
import com.wangc.report.utils.RRException;
import com.wangc.report.utils.ReportCheck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Service实现类
 *
 * @author wangc
 * @email 1620415297@qq.com
 * @date 2020-10-20 14:13:37
 */
@Service("loadService")
public class LoadServiceImpl implements LoadService {
    private static final Logger LOG = LoggerFactory.getLogger(LoadServiceImpl.class);
    @Resource
    private LoadMapper loadMapper;
    @Resource
    private ConfigMapper configMapper;
    @Autowired
    private ReportCheck reportCheck;
    @Autowired
    private TaskLogService taskLogService;

    public List<LoadEntity> queryList(Map<String, Object> map) {
        return loadMapper.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return loadMapper.queryTotal(map);
    }

    @Override
    public void loadSourceFile(String table, String dataDt, String newOldFlg) {
        ConfigEntity configEntity = configMapper.selectByPrimaryKey(Long.valueOf(table));
        TaskLogEntity taskLogEntity = new TaskLogEntity();
        taskLogEntity.setTableId(configEntity.getTableId());
        taskLogEntity.setDataDt(dataDt);
        taskLogEntity.setTaskName("导入报表源文件");
        taskLogEntity.setNewOldFlg(newOldFlg == "" ? "2" : newOldFlg);
        taskLogEntity.setProcStat("executeing");
        taskLogEntity.setState("0");
        new Thread(() -> {
            try {
                taskLogEntity.setStartDate(new Date());
                taskLogService.save(taskLogEntity);
                if ("" == newOldFlg) {
                    reportCheck.loadSourceFile(configEntity.getTableId(), dataDt, "0");
                    reportCheck.loadSourceFile(configEntity.getTableId(), dataDt, "1");
                } else {
                    reportCheck.loadSourceFile(configEntity.getTableId(), dataDt, newOldFlg);
                }
            } catch (Exception e) {
                LOG.error(e.toString());
                taskLogEntity.setProcStat("error");
                taskLogEntity.setProcInfo(e.toString());
                taskLogEntity.setFinishDate(new Date());
                taskLogService.update(taskLogEntity);
                throw new RRException(e.toString());
            }
            taskLogEntity.setProcInfo("success");
            taskLogEntity.setProcStat("finish");
            taskLogEntity.setFinishDate(new Date());
            taskLogService.update(taskLogEntity);
        }).start();
    }

}
